{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f6834a62-d124-48f6-bd87-d313b7eb5054",
   "metadata": {},
   "source": [
    "# Modulo\n",
    "\n",
    "The modulo operation (denoted as '%') returns the remainder (called \"modulus\") of a division.\n",
    "Given two numbers $a$ and $n$, the result of ($a \\% n$) is the remainder of the division of a by n.\n",
    "The modulo operation is supported only for $n = 2^m$ for an integer $m$, its result is the $m$ least significant bits.\n",
    "\n",
    "For example, the binary representation of the number $53$ is $0b110101$.\n",
    "The expression ($53 \\% 8$) equals $0b101 = 5$, because $8 = 2^3$, which means only accounting for the $3$ least\n",
    "significant bits of $53$.\n",
    "\n",
    "#### Implementation in Expressions\n",
    "\n",
    "If an expression is defined using a modulo operation, the output size is set recursively to all of its subexpressions.\n",
    "But if for some sub-expressions, another modulo operation is used,\n",
    "the sub-expression's output_size is determined by the minimal value between\n",
    "the output_size of the sub-expression and the expression.\n",
    "\n",
    "See this example: $(((a + b) \\% 4) + (c + d)) \\% 8$.\n",
    "The result of expression $a + b$ is saved on a two-qubit register,\n",
    "and the results of expressions $c + d$ and $((a + b) \\% 4) + (c + d)$ are saved\n",
    "using three qubits each."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b54ad820-bdd9-4fbe-bbe7-a7d6a3db046b",
   "metadata": {},
   "source": [
    "# Example\n",
    "\n",
    "This example generates a quantum program that adds two five-qubit arguments: a on qubits 0-4, and b on qubits 5-9.\n",
    "The adder result should have been calculated on a 6-qubit register.\n",
    "However, the modulo operation decides that the output register of the adder only contains its two least significant\n",
    "qubits. Thus, the adder result is written to a two-qubit register, on qubits 10-11."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2663dafc-22a7-43b3-838e-33829cbf04e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:12.721307Z",
     "iopub.status.busy": "2024-05-07T13:23:12.719653Z",
     "iopub.status.idle": "2024-05-07T13:23:15.527533Z",
     "shell.execute_reply": "2024-05-07T13:23:15.526703Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    inplace_prepare_int,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate(5, a)\n",
    "    allocate(5, b)\n",
    "\n",
    "    inplace_prepare_int(4, a)\n",
    "    inplace_prepare_int(7, b)\n",
    "\n",
    "    res |= (a + b) % 4\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "574fb1b6-5ef9-4e87-b6b8-c2f9a858c7ba",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:15.533106Z",
     "iopub.status.busy": "2024-05-07T13:23:15.531615Z",
     "iopub.status.idle": "2024-05-07T13:23:19.309102Z",
     "shell.execute_reply": "2024-05-07T13:23:19.308384Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'a': 4.0, 'b': 7.0, 'res': 3.0}: 1000]\n"
     ]
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"modulo_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "print(result.parsed_counts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
